@extends('admin.common.main')
@section('cnt')
    <nav class="breadcrumb">
        <i class="Hui-iconfont">&#xe67f;</i> 首页
        <span class="c-gray en">&gt;</span> 用户中心
        <span class="c-gray en">&gt;</span> 修改权限
        <a class="btn btn-success radius r" style="line-height:1.6em;margin-top:3px" href="javascript:location.replace(location.href);" title="刷新"><i class="Hui-iconfont">&#xe68f;</i></a>
    </nav>
  <article class="page-container">
      {{-- 表单验证提示 --}}
      @include('admin.common.validate')
      <form action="{{ route('admin.auth.update',$model) }}" method="post" class="form form-horizontal" @submit.prevent="dopost">
        <div class="row cl">
          <label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>是否顶级：</label>
          <div class="formControls col-xs-8 col-sm-9"> 
            <span class="select-box">
              <select v-model="selected" class="select" @change="changePid">
                  <option v-for="option in options" v-bind:value="option.id">@{{option.name}}</option>
              </select>
            </span> 
          </div>
        </div>
        <div class="row cl">
          <label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>权限名称：</label>
          <div class="formControls col-xs-8 col-sm-9">
            {{-- lazy 延时绑定 --}}
            <input type="text" class="input-text" name="name" v-model.lazy="info.name">
          </div>
        </div>
        <div class="row cl">
          <label class="form-label col-xs-4 col-sm-3">路由别名：</label>
          <div class="formControls col-xs-8 col-sm-9">
            <input type="text" class="input-text" name="route_name" v-model="info.route_name">
          </div>
        </div>
        <div class="row cl">
          <label class="form-label col-xs-4 col-sm-3">图标：</label>
          <div class="formControls col-xs-8 col-sm-9">
            <input type="text" class="input-text" name="icon" v-model="info.icon">
          </div>
        </div>
        <div class="row cl">
          <label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>是否菜单：</label>
          <div class="formControls col-xs-8 col-sm-9 skin-minimal">
            <div class="radio-box">
              <input  id="menu-1" type="radio" value="0" v-model="info.is_menu" checked>
              <label for="menu-1">否</label>
            </div>
            <div class="radio-box">
              <input id="menu-2" type="radio" value="1" v-model="info.is_menu">
              <label for="menu-2">是</label>
            </div>
          </div>
        </div>
        <div class="row cl">
          <label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>创建时间：</label>
          <div class="formControls col-xs-8 col-sm-9">
            <input type="text" class="input-text" name="created_at" v-model="info.created_at">
            <span><font color="red">可按时间排序(升序)</font></span>
          </div>
        </div>
        <div class="row cl">
          <div class="col-xs-8 col-sm-9 col-xs-offset-4 col-sm-offset-3">
            <input class="btn btn-primary radius" type="submit" value="修改权限">
            <input class="btn btn-info radius" type="button"  onclick="history.go(-1)" value="返回">
          </div>
        </div>
    </form>
  </article>
@endsection

@section('js')
    <script src="/js/vue.js"></script>
    <script>
      new Vue({
        el: '.page-container',
        data: {
          info: {
            id: "{{ $model->id }}",
            pid: "{{ $model->pid }}",
            name: "{{ $model->name }}",
            route_name: "{{ $model->route_name }}",
            icon: "{!! $model->icon !!}",
            is_menu: "{{ $model->is_menu }}",
            created_at: "{{ $model->created_at }}"
          },
          options: @json($data),  // 等同 echo json_encode($array);
          selected: {{ $model->pid ?? 0 }}  //三元简写
        },
        methods: {
        /* 方法一：
          dopost(evt) {
            let url = evt.target.getAttribute('action');
            $.post(url,this.info).then(ret=> {
              console.log(ret);
            })
          } */
          // 方法二：异步变同步 es7 async await 
          // async 在声明的函数之前，只要返回promise的地方我们就用await
          async dopost(evt) {
            let url = evt.target.action; //getAttribute('action')的简写
            if(this.info.name == ''){
              layer.msg('权限名称不能为空！',{icon:2,time:1000});
              return 
            }
            if(this.info.created_at == ''){
              layer.msg('创建时间不能为空！',{icon:2,time:1000});
              return 
            }
            let {status, msg} = await $.ajax({
              type: 'PUT',
              url: url, 
              data: {
                  'info':this.info,
                  '_token':"{{ csrf_token() }}"
              }
            });
            if(status === 0) {
              location.href="{{ route('admin.auth.index') }}";
            }else{
              layer.msg(msg,{icon:2,time:1000});
            }
          },
          //下拉 evt是dom对象
          changePid(evt) {
            // 下拉的值 获取不到默认0
            this.info.pid = evt.target.value || 0;
          }
        },
      })
    </script>

@endsection

